package org.uim.cricket.storage.queries;

import org.uim.cricket.entities.MatchPlayer;
import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.batting.BatsmanOutDetails;
import org.uim.cricket.entities.batting.BatsmanStatistics;
import org.uim.cricket.entities.batting.BattingInning;
import org.uim.cricket.entities.bowling.BowlingInning;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.storage.deserialize.clean.NormalizeBattingStatistics;

public class SinglePlayerQueryGenerator extends QueryGenerator {

	private String team = null;
	private MatchPlayer player = null;
	
	public SinglePlayerQueryGenerator(CricketMatch theMatch, String theTeam, MatchPlayer player) {
		super(theMatch);
		
		this.team = theTeam;
		this.player = player;
	}

	@Override
	public String generateQuery() throws Exception {
		StringBuilder query = new StringBuilder();
		
		String id = this.getMatch().getId().getPrefix() + "#" + this.getMatch().getId().getInternationalID();
		
		if (player.getBattingInnings() != null) {
			
			for (int b=0; b<player.getBattingInnings().size(); b++) {
				BattingInning bi = player.getBattingInnings().get(b);
				if (bi == null) continue;
				
				query.append("CALL sp_Add_BattingInning(");
				this.appendQuery(query, id, 12, false);
				this.appendQuery(query, bi.getBattedInning());
				query.append(player.getPlayer().getId()).append(",");
				this.appendQuery(query, bi.getBattedPosition());
				if (bi.getOutDetails() != null) {
					this.appendQuery(query, bi.getOutDetails().getOutType().toString(), 32, true);
					this.appendQuery(query, bi.getOutDetails().getOutAsWicket());
				} else {
					query.append("'DID_NOT_BAT', 0,");
				}
				if (bi.getStatistics() == null) {
					bi.setStatistics(new BatsmanStatistics());
					new NormalizeBattingStatistics().normalize(bi.getStatistics());
				}
				this.appendQuery(query, bi.getStatistics().getRuns());
				this.appendQuery(query, bi.getStatistics().getBalls());
				this.appendQuery(query, bi.getStatistics().getMins());
				this.appendQuery(query, bi.getStatistics().getFours());
				this.appendQuery(query, bi.getStatistics().getSixes(), false);
				query.append(");\n");
				
				BatsmanOutDetails outd = bi.getOutDetails();
				if (outd == null) continue;
				if (outd.getBowler() != null || (outd.getFielders() != null && outd.getFielders().length > 0)) {
					
					boolean toadd = true;
					if (outd.getFielders() != null && outd.getFielders().length > 0) {
						
						for (int f=0; f<outd.getFielders().length; f++) {
							Player field = outd.getFielders()[f];
							if (field == null) continue;
							if (f==1) toadd = false;
							
							query.append("CALL sp_Add_BowlerDismissal(");
							this.appendQuery(query, id, 12, false);
							this.appendQuery(query, bi.getBattedInning());
							query.append(field.getId()).append(",");
							if (outd.getBowler() == null) 
								query.append("NULL,");
							else 
								query.append(outd.getBowler().getId()).append(",");
							
							query.append(player.getPlayer().getId()).append(",");
							query.append(toadd).append(");\n");
						}
						
					} else {
						
						if (outd.getBowler() != null) {
							query.append("CALL sp_Add_BowlerDismissal(");
							this.appendQuery(query, id, 12, false);
							this.appendQuery(query, bi.getBattedInning());
							query.append("NULL,");
							query.append(outd.getBowler().getId()).append(",");
							query.append(player.getPlayer().getId()).append(",");
							query.append(toadd).append(");\n");
						}
						
					}
					
					
				}
				
				
			}
			
		}
		
		if (player.getBowlingInnings() != null) {
			
			for (int b=0; b<player.getBowlingInnings().size(); b++) {
				BowlingInning bi = player.getBowlingInnings().get(b);
				if (bi == null) continue;
				
				query.append("CALL sp_Add_BowlingInning(");
				this.appendQuery(query, id, 12, false);
				this.appendQuery(query, bi.getBowlingInning());
				query.append(player.getPlayer().getId()).append(",");
				this.appendQuery(query, bi.getBowlingPosition());
				query.append(bi.getStatistics().getOvers()).append(",");
				this.appendQuery(query, bi.getStatistics().getMaidnes());
				this.appendQuery(query, bi.getStatistics().getRuns());
				this.appendQuery(query, bi.getStatistics().getWickets());
				this.appendQuery(query, bi.getStatistics().getNoballs());
				this.appendQuery(query, bi.getStatistics().getWides(), false);
				
				query.append(");\n");

			} // end of bowling inning loop
			
		}
		
		if (player.getPlayer().isCaptain()) {
			
			query.append("CALL sp_Add_MatchCap(");
			this.appendQuery(query, id, 12, false);
			this.appendQuery(query, encodeSQL(team), 128, false);
			query.append("'C',");
			query.append(player.getPlayer().getId()).append(");\n");
			
		}
		
		if (player.getPlayer().isWicketKeeper()) {
			
			query.append("CALL sp_Add_MatchCap(");
			this.appendQuery(query, id, 12, false);
			this.appendQuery(query, encodeSQL(team), 128, false);
			query.append("'W',");
			query.append(player.getPlayer().getId()).append(");\n");
			
		}
		
		return query.toString();
	}

	public String getTeam() {
		return team;
	}

	public void setTeam(String team) {
		this.team = team;
	}

	public MatchPlayer getPlayer() {
		return player;
	}

	public void setPlayer(MatchPlayer player) {
		this.player = player;
	}

	
}
